package com.xg.controller;

import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.request.BrandReq;
import com.xg.common.response.UsedCarBrand;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌后台接口
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 品牌列表
     */
    @GetMapping("/list")
    public ApiResult<List<UsedCarBrand>> list(){
        return brandService.findCarBrandList();
    }

    /**
     * 品牌列表分页查询
     */
    @RequestMapping("/queryPageList")
    public ApiResult<PageUtil<UsedCarBrand>> queryPageList(BrandReq brandReq){
        ApiResult<PageUtil<UsedCarBrand>> pageUtilApiResult = brandService.queryPageList(brandReq);
        return pageUtilApiResult;
    }

    /**
     * 品牌列表删除结合批量和单个删除
     */
    @RequestMapping("/delete")
    public ApiResult deleteBrands(@RequestBody List<Integer> ids){
        if (!Func.isEmpty(ids)) {
            return  brandService.deleteBrands(ids);
        }
        return ApiResult.error(CommonStatusEnum.BRAND_DELETE_ERROR.getCode(), CommonStatusEnum.BRAND_DELETE_ERROR.getMsg());
    }

    /**
     * 品牌列表查询单个品牌信息
     */
    @RequestMapping("/info/{brandId}")
    public ApiResult<UsedCarBrand> selectBrandInfo(@PathVariable("brandId") Integer bannerId){
        if (!Func.isEmpty(bannerId)) {
            return brandService.selectBrandInfo(bannerId);
        }
        return ApiResult.error(CommonStatusEnum.BRAND_INFO_ERROR.getCode(), CommonStatusEnum.BRAND_INFO_ERROR.getMsg());
    }


    /**
     * 品牌信息新增和修改
     */
    @RequestMapping("/save")
    public ApiResult saveBrand(@RequestBody UsedCarBrand usedCarBrand){
        if (!Func.isEmpty(usedCarBrand)) {
            return brandService.saveBrand(usedCarBrand);
        }
        return ApiResult.error(CommonStatusEnum.BRAND_SAVE_ERROR.getCode(), CommonStatusEnum.BRAND_SAVE_ERROR.getMsg());
    }


}
